package com.zhengxing.jienci.content.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhengxing.jienci.common.pojo.CustomizePage;
import com.zhengxing.jienci.common.pojo.Information;
import com.zhengxing.jienci.common.pojo.InformationAndUser;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.content.service.InformationService;
import com.zhengxing.jienci.web.util.file.FileUploadException;
import com.zhengxing.jienci.web.util.file.FileUploadUtils;

@RestController
@RequestMapping("/information")
public class InformationController {
	
	@Autowired
	private InformationService informationService;
	
	@Autowired
	private FileUploadUtils fileUploadUtils;
	
	//关注标签ID
	public static final String ATTENTION_TAG_ID = "16050831711140011000";
	
	/**
	 * 
	 * @author yyh
	 * @Title: list
	 * @Description: 获取资讯列表数据
	 * @param @param page
	 * @param @param information
	 * @param @return
	 * @return IPage<Information>
	 * @throws
	 */
	@GetMapping("/list")
	public IPage<Information> list(CustomizePage page, Information information){
		//获取关注列表
		if(StringUtils.isNotBlank(information.getTagId()) && StringUtils.equals(information.getTagId(), ATTENTION_TAG_ID)) {
			return informationService.listAttention(page);
		}
		return informationService.list(page, information);
	}
    
    /**
     * 
     * @author yyh
     * @Title: verify
     * @Description: 审核发布信息
     * @param @param id 主键
     * @param @param state 状态
     * @param @return
     * @return int
     * @throws
     */
    @PutMapping("/verify")
    public SimpleResponse verify(String id, int state) {
    	return informationService.verify(id, state);
    };
    
    /**
     * 
     * @author yyh
     * @Title: deleteById
     * @Description: 资讯信息删除
     * @param @param id
     * @param @return
     * @return int
     * @throws
     */
    @DeleteMapping("/{id}")
    public SimpleResponse deleteById(@PathVariable String id) {
    	return informationService.deleteById(id);
    };
	
    /**
     * 
     * @author yyh
     * @Title: apply
     * @Description: 消息发布申请
     * @param @param information
     * @param @return
     * @return Object
     * @throws
     */
	@PostMapping
	public SimpleResponse apply(@RequestBody Information information) {
		return informationService.apply(information);
	}
	
	/**
	 * 
	 * @author yyh
	 * @Title: queryById
	 * @Description: 资讯详情
	 * @param @param id
	 * @param @return
	 * @return InformationAndUser
	 * @throws
	 */
	@GetMapping("/{id}")
	public InformationAndUser queryById(@PathVariable String id) {
		return informationService.queryById(id);
	}
	
	/**
	 * 
	 * @author yyh
	 * @Title: isGood
	 * @Description: 判断是否点赞
	 * @param @param id
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	@GetMapping("/isGood/{id}")
	public SimpleResponse isGood(@PathVariable String id) {
		return informationService.isGood(id);
	}
	
	/**
	 * 
	 * @author yyh
	 * @Title: giveOrCancelGood
	 * @Description: 点赞操作
	 * @param @param id
	 * @param @return
	 * @return long
	 * @throws
	 */
	@PostMapping("/giveOrCancelGood/{id}")
	public SimpleResponse giveOrCancelGood(@PathVariable String id) {
		return informationService.giveOrCancelGood(id);
	}
	
	/**
	 * 
	 * @author yyh
	 * @Title: uploadFile
	 * @Description: 文件上传
	 * @param @param uploadFile
	 * @param @return
	 * @param @throws FileUploadException
	 * @return String
	 * @throws
	 */
	@PostMapping("/upload")
	public SimpleResponse uploadFile(MultipartFile uploadFile) throws FileUploadException {
		String url = fileUploadUtils.hashCodeSaveFile(uploadFile);
		return SimpleResponse.success().put("data", "/content/upload/" + url);
	}
}
